﻿using QFramework;
using System.Collections.Generic;
using UnityEngine;

namespace HKTools
{
    public interface IHK_PanelBase
    {
        void Init();
        void Open(string message);
        void Close();
        void Pause();
        void Resume();
    }

    [RequireComponent(typeof(CanvasGroup))]
    public abstract class HK_PanelBase : HKUIFramework_BaseController, IHK_PanelBase
    {
        [Header("子页面："), SerializeField] List<HK_ChildPanelBase> childPanels;

        [HideInInspector] public HK_PanelType PanelType;
        [HideInInspector] public PanelName PanelName;
        [HideInInspector] public int Order;

        RectTransform rtf;
        protected RectTransform RTF
        {
            get
            {
                if (rtf == null)
                    rtf = GetComponent<RectTransform>();

                return rtf;
            }
        }

        CanvasGroup cg;
        protected CanvasGroup CG
        {
            get
            {
                if (cg == null)
                    cg = GetComponent<CanvasGroup>();

                return cg;
            }
        }

        IHK_UISystem uiSystem;
        protected IHK_UISystem UISystem
        {
            get
            {
                if (uiSystem == null)
                    uiSystem = this.GetSystem<IHK_UISystem>();

                return uiSystem;
            }
        }

        HK_PanelAnimBase panelAnim;
        bool isHavePanelAnim;

        protected BindableProperty<string> OpenMessage = new BindableProperty<string>();

        /// <summary>
        /// 初始化界面
        /// </summary>
        public abstract void OnInit();

        /// <summary>
        /// 显示界面
        /// </summary>
        public abstract void OnOpen();

        /// <summary>
        /// 退出界面
        /// </summary>
        public abstract void OnClose();

        /// <summary>
        /// 暂停界面
        /// </summary>
        public abstract void OnPause();

        /// <summary>
        /// 恢复界面
        /// </summary>
        public abstract void OnResume();

        public void Init()
        {
            OnInit();

            if (childPanels.Count > 0)
            {
                childPanels.ForEach(childPanel => childPanel.Init());
            }

            isHavePanelAnim = TryGetComponent<HK_PanelAnimBase>(out panelAnim);

            if (isHavePanelAnim)
                panelAnim.OnInitAnim();

            if (UISystem.IsDebugOn)
                HKDebug.Log("Init 面板：" + this.name, LogColor.Blue);

            OpenMessage.Register(value =>
            {
                HKDebug.Log("面板 " + PanelName.value.ToString() + " 开启消息：" + value, LogColor.Cyan);
            }).UnRegisterWhenGameObjectDestroyed(gameObject);
        }

        public void Open(string message)
        {
            OpenMessage.Value = message;

            OnOpen();

            if (childPanels.Count > 0)
            {
                childPanels.ForEach(childPanel => childPanel.Open());
            }

            if (isHavePanelAnim)
                panelAnim.OnOpenAnim();

            if (UISystem.IsDebugOn)
                HKDebug.Log("Open 面板：" + this.name, LogColor.Green);
        }

        public void Close()
        {
            OnClose();

            if (childPanels.Count > 0)
            {
                childPanels.ForEach(childPanel => childPanel.Close());
            }

            if (isHavePanelAnim)
                panelAnim.OnCloseAnim();

            if (UISystem.IsDebugOn)
                HKDebug.Log("Close 面板：" + this.name, LogColor.Red);
        }

        public void Pause()
        {
            OnPause();

            if (childPanels.Count > 0)
            {
                childPanels.ForEach(childPanel => childPanel.Pause());
            }

            if (isHavePanelAnim)
                panelAnim.OnPauseAnim();

            if (UISystem.IsDebugOn)
                HKDebug.Log("Pause 面板：" + this.name, LogColor.Yellow);
        }

        public void Resume()
        {
            OnResume();

            if (childPanels.Count > 0)
            {
                childPanels.ForEach(childPanel => childPanel.Resume());
            }

            if (isHavePanelAnim)
                panelAnim.OnResumeAnim();

            if (UISystem.IsDebugOn)
                HKDebug.Log("Resume 面板：" + this.name, LogColor.Green);
        }
    }
}